#include <windows.h>
#include <commctrl.h>
#include <tchar.h>

static HINSTANCE hinstance_;
static HWND label_;
static HWND btn1_;
static HWND btn2_;

//获取默认字体
HFONT GetDefaultFont(){
    static HFONT font;
	LOGFONT logfont;    
    if(font)return font;			
	if(SystemParametersInfo(SPI_GETICONTITLELOGFONT,sizeof(LOGFONT),&logfont,0)){
		font = CreateFontIndirect(&logfont);
	}else{
		font = (HFONT)GetStockObject(DEFAULT_GUI_FONT);	
	}
	return font;
}

//点击按钮时
VOID MainWndOnCommand(HWND hwnd, UINT id){
    switch (id){
    case 1: 
        SetWindowText(label_, TEXT("Button 1 clicked"));
        break;
    case 2: 
        SetWindowText(label_, TEXT("Button 2 clicked"));
        break;    
    default:
        break;
    }
}

//窗口创建时
BOOL MainWndOnCreate(HWND hwnd){    
    label_ = CreateWindow(WC_STATIC, TEXT("Hello World"), WS_CHILD|WS_VISIBLE, 10, 10, 100, 20, hwnd, NULL, hinstance_, NULL);
    SendMessage(label_, WM_SETFONT, (WPARAM)GetDefaultFont(), 0);
    btn1_ = CreateWindow(WC_BUTTON, TEXT("Button 1"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 10, 60, 80, 30, hwnd, (HMENU)1, hinstance_, NULL);
    SendMessage(btn1_, WM_SETFONT, (WPARAM)GetDefaultFont(), 0);
    btn2_ = CreateWindow(WC_BUTTON, TEXT("Button 2"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 100, 60, 80, 30, hwnd, (HMENU)2, hinstance_, NULL);
    SendMessage(btn2_, WM_SETFONT, (WPARAM)GetDefaultFont(), 0);   
    return (label_ && btn1_ && btn2_);
}

//绘制窗口
VOID MainWndOnPaint(HWND hwnd){    
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps);
    RECT rc = {10,100,200,150};
    FillRect(hdc, &rc, (HBRUSH) GetStockObject(LTGRAY_BRUSH));
    SelectObject(hdc, GetDefaultFont());
    SetTextColor(hdc, RGB(0,0,128)); 
    SetBkMode(hdc, TRANSPARENT);   
    DrawText(hdc, TEXT("Demo App hello world!"), -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
    EndPaint(hwnd, &ps);    
}

//主窗口过程
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    switch (message){
        case WM_CREATE: 
            return MainWndOnCreate(hwnd);            
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_COMMAND:
            MainWndOnCommand(hwnd, wParam);
            break;
        case WM_PAINT:
            MainWndOnPaint(hwnd);
            break;            
        case WM_CTLCOLORSTATIC:
            return (INT_PTR)GetStockObject(NULL_BRUSH);        
        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 1;
}

//注册主窗口类
BOOL RegisterWindowClass(){
    WNDCLASS wc = {0};
    wc.style = CS_VREDRAW|CS_HREDRAW;    
    wc.hIcon = LoadIcon(hinstance_, MAKEINTRESOURCE(1));
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpfnWndProc = MainWndProc;
    wc.hInstance = hinstance_;     
    wc.lpszClassName = TEXT("MainWindowClass");
    return RegisterClass(&wc);
}

//入口函数
int WINAPI _tWinMain(HINSTANCE hInst,HINSTANCE hPrev,LPTSTR lpCmdLine,int nCmdShow){
	hinstance_ = hInst;
	InitCommonControls();
	RegisterWindowClass();	
	HWND hwnd = CreateWindow(TEXT("MainWindowClass"),TEXT("Demo App"),
        (WS_OVERLAPPED|WS_CAPTION|WS_MINIMIZEBOX|WS_SYSMENU), //固定窗口大小
        //WS_OVERLAPPEDWINDOW,  //可变窗口大小
        CW_USEDEFAULT,CW_USEDEFAULT,400,300,NULL,NULL,hinstance_,NULL);		
	ShowWindow(hwnd, nCmdShow);    
    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
